<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/27
 * Time: 14:28
 */

namespace app\storage\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class OutflowModel extends Model
{
    protected $table = 'iwh_outflow';
    protected $pk = 'uuid';

    public function Storage(){
        return $this->belongsTo('StorageModel','storage_uuid','uuid')->bind('name');
    }


    /**
     * @param $condition
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOutflow($condition)
    {
        $where = $condition['where'];
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        if (empty($where['name'])) {
            $hasWhere = [];
        } else {
            $hasWhere['name'] = $where['name'];
            unset($where['name']);
        }
        $ret['total'] = $this->hasWhere('Storage', $hasWhere)->where($where)->with('Storage')->count();
        $ret['row'] = $this->hasWhere('Storage', $hasWhere)->where($where)->with('Storage')->order('time desc')->page($pageIndex, $pageSize)->select();
        return $ret;
    }

    /***根据条件获取入库流水数量
     * @param $where
     * @return int|string
     */
    public function getOutflowNum($where){
        $result = $this->where($where)->count();
        return $result;
    }

    public function getOutflowMaterielNum($where){
        $result = $this->where($where)->group("sub_materiel_id")->field("count(sub_materiel_id) num,sub_materiel_id")->order('num desc')->limit(10)->select();
        return $result;
    }
}